package com.example.administrator.orchardeveryday.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.bean.dbbean.Goods;
import com.example.administrator.orchardeveryday.dao.DaoFactory;
import com.example.administrator.orchardeveryday.utils.ImageLoaderHelper;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.List;

/**
 * Created by Administrator on 15-10-25.
 */
public class CarListAdapter extends AppBaseAdapter<Goods> {
    DisplayImageOptions displayImage;
    public CarListAdapter(List<Goods> list, Context context) {
        super(list, context);
        displayImage = ImageLoaderHelper.getDisplayImageLoader();
    }

    @Override
    public ViewHolder getViewHolder(final int position, View convertView, ViewGroup parent) {
        final Goods goods=list.get(position);
        ViewHolder vh = ViewHolder.get(context, convertView, parent, R.layout.item_car_list_goods);
        TextView title= (TextView) vh.findViewById(R.id.car_list_goods_name);
        TextView price= (TextView) vh.findViewById(R.id.car_list_goods_price);
        final TextView number= (TextView) vh.findViewById(R.id.car_list_goods_number);
        TextView numDown= (TextView) vh.findViewById(R.id.car_list_goods_down);
        TextView numUp= (TextView) vh.findViewById(R.id.car_list_goods_up);
        title.setText(goods.getGoodsName());
        price.setText(String.valueOf(goods.getPrice()));
        number.setText(String.valueOf(goods.getNumber()));
        numDown.setOnClickListener(null);
        numUp.setOnClickListener(null);
        numDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Integer.valueOf(number.getText().toString())>0)
                {
                    number.setText((Integer.valueOf(number.getText().toString())-1)+"");
                    list.get(position).setNumber(Integer.valueOf(number.getText().toString()) - 1);
                    DaoFactory.getUserDao().update(list.get(position));
                }
            }
        });

        numUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                number.setText((Integer.valueOf(number.getText().toString())+1)+"");
            }
        });
        return vh;
    }




}
